package com.vm.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.VMConst;
import com.vm.model.ShoppingCart;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class ShoppingCartAdapter extends BaseAdapter {
	private Context mContext;
	private List<ShoppingCart> list;

	public ShoppingCartAdapter(Context context, List<ShoppingCart> lists) {
		this.mContext = context;
		this.list = lists;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.shopping_cart_item, null);
			viewHolder = new ViewHolder();
			viewHolder.iv_goodsImage = (ImageView) convertView
					.findViewById(R.id.iv_goodsImage);
			viewHolder.tv_goodsName = (TextView) convertView
					.findViewById(R.id.tv_goodsName);
			viewHolder.tv_num = (TextView) convertView
					.findViewById(R.id.tv_num);
			viewHolder.tv_money = (TextView) convertView
					.findViewById(R.id.tv_money);
			viewHolder.imgbtn_add = (ImageView) convertView
					.findViewById(R.id.imgbtn_add);
			viewHolder.imgbtn_sub = (ImageView) convertView
					.findViewById(R.id.imgbtn_sub);
			viewHolder.checkbox_set = (CheckBox) convertView
					.findViewById(R.id.checkbox_set);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final ShoppingCart shoppingCart = list.get(position);
		viewHolder.imgbtn_add.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView tv_num = (TextView) v.findViewById(R.id.tv_num);
				TextView tv_money = (TextView) v.findViewById(R.id.tv_money);
				int number=Integer.parseInt(tv_num
						.getText().toString());
				tv_num.setText(Integer.toString(number+1));
				tv_money.setText(Float.toString(shoppingCart.getPrice()
						* Integer.parseInt(tv_num.getText().toString())));
			}
		});
		viewHolder.imgbtn_sub.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView tv_num = (TextView) v.findViewById(R.id.tv_num);
				TextView tv_money = (TextView) v.findViewById(R.id.tv_money);
				int number = Integer.parseInt(tv_num.getText().toString());
				if (number > 1) {
					tv_num.setText(Integer.toString(number - 1));
					tv_money.setText(Float.toString(shoppingCart.getPrice()
							* Integer.parseInt(tv_num.getText().toString())));
				}
			}
		});
		viewHolder.tv_goodsName.setTag(shoppingCart.getGoodsId());
		viewHolder.tv_goodsName.setText(shoppingCart.getName());
		viewHolder.tv_num.setText(Integer.toString(1));
		viewHolder.tv_money.setText(Float.toString(shoppingCart.getPrice()
				* Integer.parseInt(viewHolder.tv_num.getText().toString())));
		ImageLoader.getInstance().displayImage(
				VMConst.URL + shoppingCart.getImageUrl(),
				viewHolder.iv_goodsImage, MyDisplayImageOptions.getInstance());
		return convertView;
	}

	static class ViewHolder {
		ImageView iv_goodsImage;
		TextView tv_goodsName;
		TextView tv_num;
		TextView tv_money;
		ImageView imgbtn_sub;
		ImageView imgbtn_add;
		CheckBox checkbox_set;
	}
}